// pages/category/category.js

const WXAPI = require('apifm-wxapi')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    categories: [],
    showAdd: false,
    catName: '',
    shopNo: wx.getStorageSync('_curShop') && wx.getStorageSync('_curShop').shopNo
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    this.initData();
  
  },
  initData(){
    const {shopNo} = this.data
    WXAPI.findAllCat(shopNo).then(res => {
      this.setData({
        categories:res.data
      })
    })
  },
  bindCreateInput: function (e) {
    this.setData({
      catName: e.detail.value
    })
  },
  showAdd(){
    this.setData({
      showAdd: true,
      shopNo: wx.getStorageSync('_curShop') ? wx.getStorageSync('_curShop').shopNo : ''
    })
  },
  addCategory(){
    console.log(this.data.catName)
    const {shopNo, catName, categories} = this.data
    const postData = {
      shopNo: shopNo,
      itemCatName: catName,
      itemSort:categories.length + 1 + ''
    }
    WXAPI.createCategory(postData).then(res => {
      if(res.retCode === '200'){
        this.initData()
        this.setData({
          showAdd: false,
        })
      }
    })
  },
})